      $(function () {
	// loading province
	//loadProvince(true);
	// load district when province change
	$("select#inputProvinceCode").change(function() {
		
		loadDistrict(true);
	 });
	// load contact.district when contact.province change
	$("select#inputContactProvinceCode").change(function() {
		loadContactDistrict(true);
	 });
	// load city when district change
	$("select#inputDistrictCode").change(function() {
		loadCityCode(true);
	 });
	// load contact.city when contact.district change
	$("select#inputContactDistrictCode").change(function() {
		loadContactCityCode(true);
	 });
	// load contact.city when contact.district change
	$("select#inputMeterSize").change(function() {
		loadDeptAmount(true);
	 });
	setCustomerType(); // setting selected in tab1 and show in tab3
	// loading data and setting selected in tab1 and show in tab3
	loadCustomerClass();
	loadStatus(); 
	setAllowance(); // setting selected in tab1 and show in tab3
	// loading data and setting selected in tab1 and show in tab3
	loadMeterSize();
	loadMeterBrand();
	//setDefaultAddress();
	
	$("#datePickerMeterInstallDate").click(function() {
		document.getElementById("inputMeterInstallDate").focus();
	});
	
	$("#datePickerMeterReadDate").click(function() {
		document.getElementById("inputMeterReadDate").focus();
	});
});

//set Default Address
function setDefaultAddress(){
	$.getJSON('CS21001Srvl', { vaDefaultAddress: "yes", process_type:"getData" }, function(result) {
		// set Address
		$("#inputProvinceCode").val(result.vaArrayDefaultAddress[0]);
		$("#inputPostCode").val(result.vaArrayDefaultAddress[3]);
		setDistrict(result.vaArrayDefaultAddress[0],result.vaArrayDefaultAddress[1]); 
		setCityCode(result.vaArrayDefaultAddress[2],result.vaArrayDefaultAddress[0],result.vaArrayDefaultAddress[1]);			
		
		// set default Address
		$("#inputContactProvinceCode").val(result.vaArrayDefaultAddress[0]);
		$("#inputContactPostCode").val(result.vaArrayDefaultAddress[3]);
		setContactDistrict(result.vaArrayDefaultAddress[0],result.vaArrayDefaultAddress[1]); 
		setContactCityCode(result.vaArrayDefaultAddress[2],result.vaArrayDefaultAddress[0],result.vaArrayDefaultAddress[1]);	
	});
}

function setDistrict(vaProvince,vaDistrict) {
	if ( vaDistrict != null){
	    $.getJSON("CS21001Srvl", {province: vaProvince, district: "yes", process_type: "getData"}, function(result){	    	
	    	var options = '';
			options += '<option value="">-- เลือกอำเภอ --</option>';
			for (var i = 0; i < result.values.length; i++) {
				if (result.values[i] == vaDistrict){
					options += '<option value="' + result.values[i] + '" '+'selected="selected"'+'>' + result.messages[i] + '</option>';
				} else {
					options += '<option value="' + result.values[i] + '">' + result.messages[i] + '</option>';
				} 
			}
			 $("select#inputDistrictCode").html(options);
		});
	}
}

function setContactDistrict(vaProvince,vaDistrict) {
	if ( vaDistrict != null){
	    $.getJSON("CS21001Srvl", {province: vaProvince, district: "yes", process_type: "getData"}, function(result){	    	
	    	var options = '';
			options += '<option value="">-- เลือกอำเภอ --</option>';
			for (var i = 0; i < result.values.length; i++) {
				if (result.values[i] == vaDistrict){
					options += '<option value="' + result.values[i] + '" '+'selected="selected"'+'>' + result.messages[i] + '</option>';
				} else {
					options += '<option value="' + result.values[i] + '">' + result.messages[i] + '</option>';
				} 
			}
			 $("select#inputContactDistrictCode").html(options);
		});
	}
}

function setCityCode(vaCityCode, vaProvinceTemp,vaDistrictTemp) {
	if (vaCityCode != null){
	    $.getJSON("CS21001Srvl", {province: vaProvinceTemp, district: vaDistrictTemp, city: "yes", process_type:"getData" }, function(result){
	    	var options = '';
			options += '<option value="">-- เลือกตำบล --</option>';
			for (var i = 0; i < result.values.length; i++) {
				if (result.values[i] == vaCityCode){
					options += '<option value="' + result.values[i] + '" '+'selected="selected"'+'>' + result.messages[i] + '</option>';
				} else {
					options += '<option value="' + result.values[i] + '">' + result.messages[i] + '</option>';
				}
			}
		    $("select#inputCityCode").html(options);
	    });
	}
}

function setContactCityCode(vaCityCode, vaProvinceTemp,vaDistrictTemp) {
	if (vaCityCode != null){
	    $.getJSON("CS21001Srvl", {province: vaProvinceTemp, district: vaDistrictTemp, city: "yes", process_type:"getData" }, function(result){
	    	var options = '';
			options += '<option value="">-- เลือกตำบล --</option>';
			for (var i = 0; i < result.values.length; i++) {
				if (result.values[i] == vaCityCode){
					options += '<option value="' + result.values[i] + '" '+'selected="selected"'+'>' + result.messages[i] + '</option>';
				} else {
					options += '<option value="' + result.values[i] + '">' + result.messages[i] + '</option>';
				}
			}
		    $("select#inputContactCityCode").html(options);
	    });
	}
}

function loadDeptAmount(click) {
	var e = document.getElementById("inputMeterSize");
	$.getJSON('CS21001Srvl', { dept_amount: "yes", process_type: "getData" , meter_size: e.options[e.selectedIndex].value }, function(result) {
		for (var i = 0; i < result.values.length; i++) {
			document.getElementById("inputDeptAmount").value = result.messages[0];
		}
	});
}

function loadChargeRate() {
	$.getJSON('CS21001Srvl', { charge_rate: "yes", process_type: "getData" }, function(result) {
		// inputGarbageCharge, inputWasteWaterCharge, inputOtherCharge
		document.getElementById("inputGarbageCharge").value = result.messages[0];
		document.getElementById("inputWasteWaterCharge").value = result.messages[1];
		document.getElementById("inputOtherCharge").value = result.messages[2];
	});
}

function loadProvince(click) {
	$.getJSON('CS21001Srvl', { province: "yes", process_type: "getData" }, function(result) {
		var options = '';
		options += '<option value="">-- เลือกจังหวัด --</option>';
		for (var i = 0; i < result.values.length; i++) {
			options += '<option value="' + result.values[i] + '">' + result.messages[i] + '</option>';
		}
	    $("select#inputProvinceCode").html(options);
	    $("select#inputContactProvinceCode").html(options);
	    if (click != true)
	    	setProvinceSelected();
	});
}

function loadDistrict(click) {
	var e = document.getElementById("inputProvinceCode");
    $.getJSON("CS21001Srvl", {province: e.options[e.selectedIndex].value, district: "yes", process_type: "getData"}, function(result){
    	var options = '';
		options += '<option value="">-- เลือกอำเภอ --</option>';
		for (var i = 0; i < result.values.length; i++) {
			options += '<option value="' + result.values[i] + '">' + result.messages[i] + '</option>';
		}
	    $("select#inputDistrictCode").html(options);
	    // set default to city
	    var options2 = '<option value="">-- เลือกตำบล --</option>';
		$("select#inputCityCode").html(options2);
		if (click != true)
			setDistrictSelected();
    });
}

function loadContactDistrict(click) {
	var e = document.getElementById("inputContactProvinceCode");
    $.getJSON("CS21001Srvl", {province: e.options[e.selectedIndex].value, district: "yes", process_type: "getData"}, function(result){
    	var options = '';
		options += '<option value="">-- เลือกอำเภอ --</option>';
		for (var i = 0; i < result.values.length; i++) {
			options += '<option value="' + result.values[i] + '">' + result.messages[i] + '</option>';
		}
	    $("select#inputContactDistrictCode").html(options);
	    // set default to city
	    var options2 = '<option value="">-- เลือกตำบล --</option>';
		$("select#inputContactCityCode").html(options2);
		if (click != true)
			setContactDistrictSelected();
    });
}

function loadCityCode(click) {
	var vaDistrict = document.getElementById("inputDistrictCode");
	var vaProvince = document.getElementById("inputProvinceCode");
    $.getJSON("CS21001Srvl", {province: vaProvince.options[vaProvince.selectedIndex].value, 
    	district: vaDistrict.options[vaDistrict.selectedIndex].value, city: "yes", process_type: "getData"}, function(result){
    	var options = '';
		options += '<option value="">-- เลือกตำบล --</option>';
		for (var i = 0; i < result.values.length; i++) {
			options += '<option value="' + result.values[i] + '">' + result.messages[i] + '</option>';
		}
	    $("select#inputCityCode").html(options);
	    if (click != true)
	    	setCityCodeSelected();
    });
}

function loadContactCityCode(click) {
	var vaDistrict = document.getElementById("inputContactDistrictCode");
	var vaProvince = document.getElementById("inputContactProvinceCode");
    $.getJSON("CS21001Srvl", {province: vaProvince.options[vaProvince.selectedIndex].value, 
    	district: vaDistrict.options[vaDistrict.selectedIndex].value, city: "yes", process_type: "getData"}, function(result){
    	var options = '';
		options += '<option value="">-- เลือกตำบล --</option>';
		for (var i = 0; i < result.values.length; i++) {
			options += '<option value="' + result.values[i] + '">' + result.messages[i] + '</option>';
		}
	    $("select#inputContactCityCode").html(options);
	    if (click != true)
	    	setContactCityCodeSelected();
    });
}

function loadCustomerClass() {
	$.getJSON('CS21001Srvl', { customer_class: "yes", process_type: "getData" }, function(result) {
		var options = '';
		options += '<option value="">-- เลือก --</option>';
		for (var i = 0; i < result.values.length; i++) {
			options += '<option value="' + result.values[i] + '">' + result.messages[i] + '</option>';
		}
	    $("select#inputCustomerClass").html(options);
	    setCustomerClass();
	});
}

function loadStatus() {
	$.getJSON('CS21001Srvl', { status: "yes", process_type: "getData" }, function(result) {
		var options = '';
		options += '<option value="">-- เลือก --</option>';
		for (var i = 0; i < result.values.length; i++) {
			options += '<option value="' + result.values[i] + '">' + result.messages[i] + '</option>';
		}
	    $("select#inputStatus").html(options);
	    setStatus();
	});
}

function loadMeterSize() {
	$.getJSON('CS21001Srvl', { meter_size: "yes", process_type: "getData" }, function(result) {
		var options = '';
		options += '<option value="">-- เลือก --</option>';
		for (var i = 0; i < result.values.length; i++) {
			options += '<option value="' + result.values[i] + '">' + result.messages[i] + '</option>';
		}
	    $("select#inputMeterSize").html(options);
	    setMeterSize();
	});
}

function loadMeterBrand() {
	$.getJSON('CS21001Srvl', { meter_brand: "yes", process_type: "getData" }, function(result) {
		var options = '';
		options += '<option value="">-- เลือก --</option>';
		for (var i = 0; i < result.values.length; i++) {
			options += '<option value="' + result.values[i] + '">' + result.messages[i] + '</option>';
		}
	    $("select#inputMeterBrand").html(options);
	    setMeterBrand();
	});
}

function showProvince() {
	var voInput = document.getElementById("inputProvinceCode");
	if (voInput.selectedIndex == 0) {
	 	document.getElementById("show_province").innerHTML = "";
	} else {
		document.getElementById("show_province").innerHTML = voInput.options[voInput.selectedIndex].text;
	}
	var voInput2 = document.getElementById("inputContactProvinceCode");
	if (voInput2.selectedIndex == 0) {
	 	document.getElementById("show_contact_province").innerHTML = "";
	} else {
		document.getElementById("show_contact_province").innerHTML = voInput2.options[voInput2.selectedIndex].text;
	}
}

function showDistrict() {
	var voInput = document.getElementById("inputDistrictCode");
	if (voInput.selectedIndex == 0) {
	 	document.getElementById("show_district").innerHTML = "";
	} else {
		document.getElementById("show_district").innerHTML = voInput.options[voInput.selectedIndex].text;
	}
}

function showContactDistrict() {
	var voInput = document.getElementById("inputContactDistrictCode");
	if (voInput.selectedIndex == 0) {
	 	document.getElementById("show_contact_district").innerHTML = "";
	} else {
		document.getElementById("show_contact_district").innerHTML = voInput.options[voInput.selectedIndex].text;
	}
}

function showCityCode() {
	var voInput = document.getElementById("inputCityCode");
	if (voInput.selectedIndex == 0) {
	 	document.getElementById("show_city_code").innerHTML = "";
	} else {
		document.getElementById("show_city_code").innerHTML = voInput.options[voInput.selectedIndex].text;
	}
}

function showContactCityCode() {
	var voInput = document.getElementById("inputContactCityCode");
	if (voInput.selectedIndex == 0) {
	 	document.getElementById("show_contact_city_code").innerHTML = "";
	} else {
		document.getElementById("show_contact_city_code").innerHTML = voInput.options[voInput.selectedIndex].text;
	}
}

function showCustomerType() {
	var voInput = document.getElementById("inputCustomerType");
	if (voInput.selectedIndex == 0) {
	 	document.getElementById("show_customer_type").innerHTML = "";
	} else {
		document.getElementById("show_customer_type").innerHTML = voInput.options[voInput.selectedIndex].text;
	}
}

function showCustomerClass() {
	var voInput = document.getElementById("inputCustomerClass");
	if (voInput.selectedIndex == 0) {
	 	document.getElementById("show_customer_class").innerHTML = "";
	} else {
		document.getElementById("show_customer_class").innerHTML = voInput.options[voInput.selectedIndex].text;
	}
}

function showStatus() {
	var voInput = document.getElementById("inputStatus");
	if (voInput.selectedIndex == 0) {
	 	document.getElementById("show_status").innerHTML = "";
	} else {
		document.getElementById("show_status").innerHTML = voInput.options[voInput.selectedIndex].text;
	}
}

function showAllowance() {
	var voInput = document.getElementById("inputAllowance");
	if (voInput.selectedIndex == 0) {
	 	document.getElementById("show_allowance").innerHTML = "";
	} else {
		document.getElementById("show_allowance").innerHTML = voInput.options[voInput.selectedIndex].text;
	}
}

function showMeterSize() {
	var voInput = document.getElementById("inputMeterSize");
	if (voInput.selectedIndex == 0) {
	 	document.getElementById("show_meter_size").innerHTML = "";
	} else {
		document.getElementById("show_meter_size").innerHTML = voInput.options[voInput.selectedIndex].text;
	}
}

function showMeterBrand() {
	var voInput = document.getElementById("inputMeterBrand");
	if (voInput.selectedIndex == 0) {
	 	document.getElementById("show_meter_brand").innerHTML = "";
	} else {
		document.getElementById("show_meter_brand").innerHTML = voInput.options[voInput.selectedIndex].text;
	}
}

//-------- check button click
function checkButtonClick(button) {
	if (button == "user") {
		document.getElementById("button_user").value = "yes";
	} else if (button == "meter") {
		document.getElementById("button_meter").value = "yes";
	}	
}

//------- validation data before summit
// literal syntax เช่น /pattern/ 
function validateForm() {
	var error = false;
	$("#inputContactPostCode").popover('destroy');
	$("#inputPostCode").popover('destroy');
	$("#inputTaxId").popover('destroy');
	$("#inputCardId").popover('destroy');
	if (document.getElementById("inputContactPostCode").value != "") {
		var numbers = /^[ ]*[0-9]{5}[ ]*$/;
		if (!numbers.test(document.getElementById("inputContactPostCode").value)) {
			$("#inputContactPostCode").focus();
			
			$("#inputContactPostCode").popover('show');
			error = true;
		}
	}
	if (document.getElementById("inputPostCode").value != "") {
		var numbers = /^[ ]*[0-9]{5}[ ]*$/;
		if (!numbers.test(document.getElementById("inputPostCode").value)) {
			$("#inputPostCode").focus();
			$("#inputPostCode").popover('show');
			error = true;
		}
	}
	if (document.getElementById("inputTaxId").value != "") {
		var numbers = /^[ ]*[0-9]{13}[ ]*$/;
		if (!numbers.test(document.getElementById("inputTaxId").value)) {
			$("#inputTaxId").focus();
			$("#inputTaxId").popover('show');
			error = true;
		}
	}
	if (document.getElementById("inputCardId").value != "") {
		// var numbers = /^[ ]*[0-9]{13}[ ]*$/;
		// !numbers.test(document.getElementById("inputCardId").value) && 
		if (!checkCardID(document.getElementById("inputCardId").value)) {
			$("#inputCardId").focus();
			$("#inputCardId").popover('show');
			error = true;
		}
	}
	if (error) return false;
	else return true;
}

function checkCardID(id) {
	if (id.length != 13) 
		return false;
	for (i = 0, sum = 0; i < 12; i++)
			sum += parseFloat(id.charAt(i)) * (13-i); 
	if ((11 - sum % 11) % 10 != parseFloat(id.charAt(12)))
		return false;
	return true;
}